﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data;

/// <summary>
///ResourceReco 的摘要说明
/// </summary>
public class ResourceReco
{
    int rid = 0;
    int sid = 0;
    double downloadRank = 0;
    double preferenceRank = 0;
    double timeRank = 0;
    double diffRank = 0;
    DBaccess db = new DBaccess();
	public ResourceReco()
	{
        
	}

    public ResourceReco(int sid, int rid)
    {
        this.sid = sid;
        this.rid = rid;
        downloadRank = GetDownload();
        preferenceRank = GetPreference();
        timeRank = GetTime();
        diffRank = GetDiff();
    }

    public double GetDiffRank()
    {
        return diffRank;
    }

    public double GetDownloadRank()
    {
        return downloadRank;
    }

    public double GetDownloadScore()
    {
        DataSet ds = new DataSet();
        ds = db.GetDataSet("select count(*) from mr_resource");
        double count = double.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1;
        if (downloadRank == 0) downloadRank = count;
        return (count - downloadRank) / count * 100 ;
    }

    public double GetPreferenceScore()
    {
        DataSet ds = new DataSet();
        ds = db.GetDataSet("select count(*) from mr_resource");
        double count = double.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1;
        if (preferenceRank == 0) preferenceRank = count;
        return (count - preferenceRank) / count * 100;
    }

    public double GetTimeScore()
    {
        DataSet ds = new DataSet();
        ds = db.GetDataSet("select count(*) from mr_resource");
        double count = double.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1;
        if (timeRank == 0) timeRank = count;
        return (count - timeRank) / count * 100;
    }

    public double GetDiffScore()
    {
        DataSet ds = new DataSet();
        ds = db.GetDataSet("select count(*) from mr_resource");
        double count = double.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1;
        if (diffRank == 0) diffRank = count;
        return (count - diffRank) / count * 100;
    }

    public double GetPreferenceRank()
    {
        return preferenceRank;
    }
    public double GetTimeRank()
    {
        return timeRank;
    }

    private double GetDiff()
    {
        DataSet ds1 = new DataSet();
        DataSet ds2 = new DataSet();
        DataSet ds3 = new DataSet();
        DataSet ds4 = new DataSet();
        ArrayList arr = new ArrayList();
        String sql1 = "select score from mr_resource where rid = '" + rid + "'";
        String sql2 = "select value from mr_parameter where pname = 'GPAperscore'";
        String sql3 = "select score from mr_resource";
        String sql4 = "select GPA from mr_student where sid = '" + sid + "'";
        ds1 = db.GetDataSet(sql1);
        ds2 = db.GetDataSet(sql2);
        ds3 = db.GetDataSet(sql3);
        ds4 = db.GetDataSet(sql4);
        double expect = double.Parse(ds2.Tables[0].Rows[0][0].ToString()) * double.Parse(ds4.Tables[0].Rows[0][0].ToString());
        double current = Math.Abs(expect - double.Parse(ds1.Tables[0].Rows[0][0].ToString()));
        double temp = 0;
        arr.Add(current);
        int i;
        int same = 1;
        for (i = 0; i < ds3.Tables[0].Rows.Count; i++)
        {
            temp = Math.Abs(expect - double.Parse(ds3.Tables[0].Rows[i][0].ToString()));
            arr.Add(temp);
            if (temp == current) same++;
        }
        arr.Sort();
        int begin = arr.IndexOf(current) + 1;
        arr.Reverse();
        int end = arr.IndexOf(current) + 1;
        return (double)(begin + end) / same;
    }

    private double GetDownload()
    {
        DataSet ds1 = new DataSet();
        ArrayList arr = new ArrayList();
        String sql = "select * from (select a.rank,a.rid from (select *,rank() over (order by num desc) as rank from mr_v_downloadcount) a) b where b.rank = (select rank from (select *,rank() over (order by num desc) as rank from mr_v_downloadcount) a where a.rid = '" + rid + "')";
        ds1 = db.GetDataSet(sql);
        int same = ds1.Tables[0].Rows.Count;
        if (ds1.Tables[0].Rows.Count == 0)
            return 0;
        else
        {
            int count = int.Parse(ds1.Tables[0].Rows[0][0].ToString());
            return (count + same - 1) / same;
        }
    }

    private double GetPreference()
    {
        DataSet ds1 = new DataSet();
        DataSet ds2 = new DataSet();
        DataSet ds3 = new DataSet();
        ArrayList arr = new ArrayList();
        String sql1 = "select score from mr_resource where rid = '" + rid + "'";
        String sql2 = "select score from mr_download a ,mr_resource b where a.rid = b.rid and a.sid ='" + sid + "'";
        String sql3 = "select score from mr_resource";
        ds1 = db.GetDataSet(sql1);
        ds2 = db.GetDataSet(sql2);
        ds3 = db.GetDataSet(sql3);
        int i = 0 , j = 0;
        double r = 0, temp = 0;
        int same = 1;
        if (ds2.Tables[0].Rows.Count == 0)
            return 0;
        else
        {
            for (i = 0; i < ds2.Tables[0].Rows.Count; i++)
            {
                r += (Double.Parse(ds1.Tables[0].Rows[0][0].ToString()) - Double.Parse(ds2.Tables[0].Rows[i][0].ToString())) * (Double.Parse(ds1.Tables[0].Rows[0][0].ToString()) - Double.Parse(ds2.Tables[0].Rows[i][0].ToString()));
            }
            arr.Add(r);
            for (i = 0; i < ds3.Tables[0].Rows.Count; i++)
            {
                temp = 0;
                for (j = 0; j < ds2.Tables[0].Rows.Count; j++)
                {
                    temp += (Double.Parse(ds3.Tables[0].Rows[i][0].ToString()) - Double.Parse(ds2.Tables[0].Rows[j][0].ToString())) * (Double.Parse(ds3.Tables[0].Rows[i][0].ToString()) - Double.Parse(ds2.Tables[0].Rows[j][0].ToString()));
                }
                if (r == temp) same++;
                arr.Add(temp);
            }
            arr.Sort();
            int begin = arr.IndexOf(r) + 1;
            arr.Reverse();
            int end = arr.IndexOf(r) + 1;
            return (double)(begin + end) / same;
        }
    }

    private double GetTime()
    {
        DataSet ds = new DataSet();
        String sql = "select a.rank from (select *, rank() over (order by upload_time desc) as rank from mr_resource) a where a.rid = '" + rid + "'";
        ds = db.GetDataSet(sql);
        return double.Parse(ds.Tables[0].Rows[0][0].ToString());
    }

    public double GetAll()
    {
        double result = 0;
        DataSet ds1 = new DataSet();
        DataSet ds2 = new DataSet();
        DataSet ds3 = new DataSet();
        DataSet ds4 = new DataSet();
        DataSet ds5 = new DataSet();
        String sql1 = "select download_weight from mr_member where mid = '" + sid + "'";
        String sql2 = "select preference_weight from mr_member where mid = '" + sid + "'";
        String sql3 = "select time_weight from mr_member where mid = '" + sid + "'";
        String sql4 = "select count(*) from mr_resource";
        String sql5 = "select difficulty_weight from mr_member where mid = '" + sid + "'";
        ds1 = db.GetDataSet(sql1);
        ds2 = db.GetDataSet(sql2);
        ds3 = db.GetDataSet(sql3);
        ds4 = db.GetDataSet(sql4);
        ds5 = db.GetDataSet(sql5);
        double count = double.Parse(ds4.Tables[0].Rows[0][0].ToString()) + 1;
        if (downloadRank == 0) downloadRank = count;
        if (preferenceRank == 0) preferenceRank = count;
        if (timeRank == 0) timeRank = count;
        if (diffRank == 0) diffRank = count;
        result = double.Parse(ds1.Tables[0].Rows[0][0].ToString()) * (count - downloadRank) + double.Parse(ds2.Tables[0].Rows[0][0].ToString()) * (count - preferenceRank) + double.Parse(ds3.Tables[0].Rows[0][0].ToString()) * (count - timeRank) + double.Parse(ds5.Tables[0].Rows[0][0].ToString()) * (count - diffRank);
        return result / count * 100;
    }
}